package com.gp.geits.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.gp.geits.utils.PageUtils;
import com.gp.geits.utils.R;
import com.gp.geits.vo.AnswerSubmitVo;
import com.gp.geits.vo.IssueAndAnswerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gp.geits.entity.ResponseEntity;
import com.gp.geits.service.ResponseService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author lzf
 * @date 2022-03-09 15:36:11
 */
@RestController
@RequestMapping("/response")
public class ResponseController {
    @Autowired
    private ResponseService responseService;

    /**
     * 提交问卷回答
     */
    @PostMapping("/answerSubmit")
    public R answerSubmit(@RequestBody List<AnswerSubmitVo> submitVoList,
                          HttpServletRequest request){
        responseService.answerSubmit(submitVoList,request);
        return R.ok().message("提交成功");
    }

    /**
     * 根据用户id查出问卷
     */
    @GetMapping("/checkQuestionnaireByUserId")
    public R checkQuestionnaireByUserId(HttpServletRequest request){
        List<String> questionnaireList = responseService.checkQuestionnaireByUserId(request);
        return R.ok().data("questionnaireList",questionnaireList);
    }

    /**
     * 根据问卷名称和用户id列出问题及回答
     */
    @GetMapping("/issueAndAnswerByQuestionnaireNameAndUserId/{name}")
    public R issueAndAnswerByQuestionnaireName(@PathVariable("name") String name,
                                               HttpServletRequest request){
        IssueAndAnswerVo issueAndAnswerVo = responseService.issueAndAnswerByQuestionnaireName(name,request);
        return R.ok().data("issueAndAnswerVo",issueAndAnswerVo);
    }

    /**
     * 所有问题及回答
     */
    @GetMapping("/issueAndAnswerList")
    public R issueAndAnswerList(){
        List<IssueAndAnswerVo> issueAndAnswerVoList = responseService.issueAndAnswerList();
        return R.ok().data("issueAndAnswerVoList",issueAndAnswerVoList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = responseService.queryPage(params);

        return R.ok().data("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		ResponseEntity response = responseService.getById(id);

        return R.ok().data("response", response);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ResponseEntity response){
		responseService.save(response);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ResponseEntity response){
		responseService.updateById(response);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		responseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
